global FIRST_YEAR 1964
global FINAL_YEAR 2016
global dropbox_dir "C:\Users\yuxus\Dropbox\PROJECTS (ACTIVE)\Bond.Risk.Premia.Mitra.Xu\Data\PRIOR_TO_RFS_RR\"

/*
1. Yields
*/

use "${dropbox_dir}combined_results_yu\data\fama_bliss_prices.dta", clear
gen D = mod(Date_yyyymmdd, 100)
gen M = mod((Date_yyyymmdd - D)/100, 100)
gen Y = (Date_yyyymmdd - 100*M - D)/10000
gen datem = ym(Y,M)
format datem %tm

keep if datem>=ym(${FIRST_YEAR},1) & datem<=ym(${FINAL_YEAR},12)

local ave_yld = 0
local ave_sd = 0

forvalues year = 1/5{
	gen yld_`year'y = -100*log(price_`year'y/100)/`year'
	sum yld_`year'y
	local ave_yld = `ave_yld' + r(mean)
	local ave_sd = `ave_sd' + r(sd)
}

sum yld_*

local ave_yld = `ave_yld'/5
local ave_sd = `ave_sd'/5

disp `ave_yld'
disp `ave_sd'

sum yld_*

/*
* 4. Bond (log) holding period returns
*/

freduse USREC, clear
gen datem = ym(year(daten), month(daten))
format datem %tm
keep datem USREC
save temp_USREC.dta


use "${dropbox_dir}combined_results_yu\data\fama_bliss_prices.dta", clear
gen D = mod(Date_yyyymmdd, 100)
gen M = mod((Date_yyyymmdd - D)/100, 100)
gen Y = (Date_yyyymmdd - 100*M - D)/10000
gen datem = ym(Y,M)
format datem %tm

merge 1:1 datem using temp_USREC.dta
keep if _merge==3
drop _merge

sleep 5000
erase temp_USREC.dta

tsset datem
keep if datem>=ym(${FIRST_YEAR},1) & datem<=ym(${FINAL_YEAR},12)

forvalues n = 2/5{
    local nsell = `n' - 1
    gen hpxr_`n'y = log(price_`nsell'y/100) - log(L12.price_`n'y/100) + log(L12.price_1y/100)
}

sum hpxr*